package com.hzy.util;


import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.*;


public class MarkdownUtils {

    /**
     * markdown格式转HTML
     * @param markdown
     * @return
     */
    public static  String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     * 增加扩展（标题，表格生成）
     * Markdown转换成HTMl
     * @param markdown
     * @return
     */
    public static  String markdownToHtmlExtensions(String markdown){
        //h标题生成id
        Set<Extension> headingAnchorExtensions= Collections.singleton(HeadingAnchorExtension.create());
        //转换tableHtml
        List<Extension> tableExtension= Arrays.asList(TablesExtension.create());
        Parser parser= Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document=parser.parse(markdown);
        HtmlRenderer renderer=HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    @Override
                    public AttributeProvider create(AttributeProviderContext attributeProviderContext) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     * 处理标签属性
     */
    static  class CustomAttributeProvider implements AttributeProvider{

    @Override
    public void setAttributes(Node node, String s, Map<String, String> attributes) {
        //改变a标签taget属性为_blank
        if(node instanceof Link){
            attributes.put("target","_blank");
        }
        if(node instanceof TableBlock){
            attributes.put("class","ui celled table");
        }
    }
}

    /**
     * 测试
     */
    public static void main(String[] args) {
        String table="|hello|hi |asda|asdasdkmask|\n"+
                "|hello|hi |asda|asdasdkmask|\n";
        String a="[imcoding 爱编辑](http://www.baidu.com)";
        System.out.println(markdownToHtmlExtensions(table));
    }


}
